package ar.edu.unq.tpi.uis.tepe3.wicket.model;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import ar.edu.unq.tpi.uis.tepe3.dominio.Folder;
import ar.edu.unq.tpi.uis.tepe3.dominio.User;
import ar.edu.unq.tpi.uis.tepe3.persistencia.FolderDAO;
import ar.edu.unq.tpi.uis.tepe3.persistencia.HibernateUtil;
import ar.edu.unq.tpi.uis.tepe3.persistencia.UserDAO;

public class FolderList {
	
	private Long id;
	private String name;
	private String routeString;
	private Folder father;
	
	private List<Folder> result;
	
	public FolderList(){
		super();
		this.loadAllFolders();
	}
	
	public void search() {
		this.result = this.getFolderDAO().findByExample(this.getExample());
		this.getFolderDAO().flush();
	}

	protected Folder getExample() {
		return new Folder(this.getName());
	}

	public void clean() {
		this.result.clear();
	}

	public void add(Folder folder) {
		//Videoclub.getHome(Socio.class).agregar(socio);
		//this.search();
	}
	
	public void loadAllFolders(){
//		Session sess = HibernateUtil.getSessionFactory().openSession();
//		sess.beginTransaction();
//		this.result = sess.createCriteria("ar.edu.unq.tpi.uis.tepe3.dominio.Folder").list();
//		sess.getTransaction().commit();
		Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = null;
		try {
			tx = sess.beginTransaction();
			this.result  = sess.createCriteria(Folder.class).list();
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally{
			if (sess.isOpen()) sess.close();
		}
	}

	public void delete(Folder folder) {
		Session sess = HibernateUtil.getSessionFactory().openSession();
		sess.beginTransaction();
		sess.refresh(folder);
		sess.delete(folder);
		sess.getTransaction().commit();
		
		this.loadAllFolders();
	}

	public FolderDAO getFolderDAO() {
		return new FolderDAO();
	}
	
	//********************
	// GETTERS Y SETTERS
	//********************

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRouteString() {
		return routeString;
	}

	public Folder getFather() {
		return father;
	}

	public void setFather(Folder father) {
		this.father = father;
	}

	public List<Folder> getResult() {
		return result;
	}

	public void setResult(List<Folder> result) {
		this.result = result;
	}
	
	
	
}
